import { Component, OnInit, Input, Injector } from '@angular/core';
import { CoreComponent } from 'pengesoft-ng-lib';
import { ResumeMgeSvr } from '../../../services/resume-mge-svr.service';
import { Resume } from '../../../domains/resume.domain';
import { NzModalService } from 'ng-zorro-antd/modal';
import { JobServiceSvr } from '../../../services/job.service';
import { AddCandidateComponent } from 'src/app/components/add-candidate/add-candidate.component';
import { identifierModuleUrl } from '@angular/compiler';

@Component({
  selector: 'app-candidate',
  templateUrl: './candidate.component.html',
  styleUrls: ['./candidate.component.less']
})

export class CandidateComponent extends CoreComponent implements OnInit {
  resumeInfoList: any;
  selectvalue: any;
  selectApplyType = null;  // 招聘方式查询
  selectJobId = null;   // 招聘岗位查询
  @Input() allJobs: any;
  page = 0;          // 0:新候选人页  1：初试页  2： 复试页  3：加试页   4：通过   5：淘汰页   6：回拒页
  thisData = new Resume();
  @Input() info: any;
  temp = {
    Resume: new Resume(),
    JobName: '',
  };
  state: any = ['进入初试', '进入复试', '进入加试', '通过，请到待入职处处理', '淘汰', '回拒'];
  i: any;


  // 控制按钮换类的状态
  classstatus: any = [
    true,
    false,
    false,
    false,
    false,
    false,
  ];
  listOfOption2 = [
    { label: '校招', value: 1 },
    { label: '社招', value: 2 },
    { label: '猎头', value: 3 },
  ];

  constructor(
    public jobServiceSvr: JobServiceSvr,
    private modal: NzModalService,
    private injector: Injector,
    public resumeSvr: ResumeMgeSvr) {
    super(injector);
  }

  ngOnInit(): void {
  }
  // 按钮点击事件，点击后发送请求并切换样式
  clickEvent(n): void {
    this.page = n;
    this.changeClass(n);
    this.resumeSvr.getDetailList(new Resume()).then(res => {
      this.resumeInfoList = res.filter(item => {
        return item.Resume.ApplyStatus === n + 1;
      });
      this.info = this.resumeInfoList;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

// 两个下拉筛选
  onChangesStatus(i , value): void{
    console.log(value);
    if ( i === 1 ) {
      // 岗位ID
      this.selectJobId = value;
    }
    if ( i === 2 ) {
      // 招聘类型
      this.selectApplyType = value;
    }
    this.thisData.ApplyType = this.selectApplyType;
    this.thisData.JobId = this.selectJobId;
    // 获取该行数据
    this.resumeSvr.getDetailList(this.thisData).then(res => {
      console.log(res);
      this.resumeInfoList = res.filter(item => {
        return item.Resume.ApplyStatus === this.page + 1;
      });
      console.log(this.resumeInfoList);
      this.info = this.resumeInfoList;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }
  // 更换按钮样式
  changeClass(n): void {
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0; i < this.classstatus.length; i++) {
      if (i === n) {
        this.classstatus[i] = true;
      } else {
        this.classstatus[i] = false;
      }
    }
  }

  open(i, data?: any): void {
      if (i === 1) {
        this.temp.Resume = new Resume();
        this.turnNew('新增候选人', this.temp , this.page );
      }
      if (i === 2) {
        this.thisData.Id = data.Resume.Id;
        // 获取该行数据
        this.resumeSvr.getDetailList(this.thisData).then(res => {
          this.turnNew('修改', res[0] , this.page );
        }, err => {
          this.nzMessageSvr.error(err.Msg);
        });
      }

   }

  // 操作选择
  select(e): void {
    this.selectvalue = e;
  }
  // 打开进入初试 确认框
  showConfirm(i, data): void {
    this.temp = data;
    console.log(data);
    console.log(this.temp);
    this.modal.confirm({
      nzTitle: '<i>确认' + this.temp.Resume.Name + '<em style="color:red">' + this.state[i - 2] + '?</em></i>',
      nzComponentParams: {
        i: this.i,
        data: this.temp,
      },
      nzOnOk: () => {
        data.Resume.ApplyStatus = i;
        this.resumeSvr.updateResume(new Resume(data.Resume)).then(res => {
          // tslint:disable-next-line: triple-equals
          if (i == 7) {
            this.clickEvent(this.page);
          } else {
            this.clickEvent(i - 1);
          }
        }, err => {
          this.nzMessageSvr.error(err.Msg);
        });
      }
    });
  }

  // 新的页面
  turnNew(title, data, page): void {
    const edt = this.modal.create({
      nzTitle: title,
      nzContent: AddCandidateComponent,
      nzFooter: null,
      nzComponentParams: {
        data,
        page,
        allJobs: this.allJobs,
      }
    });
    // tslint:disable-next-line: no-shadowed-variable
    edt.afterClose.subscribe((page) => {
      this.clickEvent(page);
    });
  }
}
